const state = {
  // 最后一次导入结果
  lastImportResult: null,
  // 导入历史记录 (保留最近10次)
  importHistory: []
}

const mutations = {
  // 设置导入结果
  SET_IMPORT_RESULT(state, result) {
    state.lastImportResult = {
      ...result,
      timestamp: new Date().getTime(),
      id: Date.now() + Math.random()
    }

    // 添加到历史记录
    state.importHistory.unshift(state.lastImportResult)

    // 只保留最近10次记录
    if (state.importHistory.length > 10) {
      state.importHistory = state.importHistory.slice(0, 10)
    }
  },

  // 清除导入结果
  CLEAR_IMPORT_RESULT(state) {
    state.lastImportResult = null
  },

  // 清除历史记录
  CLEAR_IMPORT_HISTORY(state) {
    state.importHistory = []
  }
}

const actions = {
  // 保存导入结果
  saveImportResult({ commit }, result) {
    commit('SET_IMPORT_RESULT', result)
  },

  // 清除导入结果
  clearImportResult({ commit }) {
    commit('CLEAR_IMPORT_RESULT')
  },

  // 清除历史记录
  clearImportHistory({ commit }) {
    commit('CLEAR_IMPORT_HISTORY')
  }
}

const getters = {
  // 获取最后一次导入结果
  lastImportResult: state => state.lastImportResult,

  // 获取导入历史记录
  importHistory: state => state.importHistory,

  // 检查是否有失败记录
  hasFailedRecords: state => {
    return state.lastImportResult &&
           state.lastImportResult.failedRecords &&
           state.lastImportResult.failedRecords.length > 0
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
